/**
 *
 */
package com.honeybees.framework.datatables.ajax;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * <dl>
 * <dt><b> datatables 请求数据结构 </b></dt>
 * <p>
 * <dd><b>https://www.datatables.net/manual/server-side Sent parameters</b></dd>
 * </dl>
 * <p>
 * Copyright (C) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @@since 2017-04-07 17:20:13
 */
public class DatatablesRequest implements Serializable {

    /**
     * 序列化版本号
     */
    private static final long serialVersionUID = -2112309013774597800L;

    /**
     * columns[i][data]
     */
    public static final Pattern PATTERN_columns = Pattern.compile("columns\\[([0-9]*)?\\]");
    /**
     * order[i][column]
     */
    //public static final Pattern PATTERN_order = Pattern.compile("order\\[([0-9]*)?\\]");

    /**
     * 浏览器cache的编号，递增不可重复
     * <p>
     * 用来保证从服务器端异步返回的结果按照序列绘制，可以有效防止跨域攻击。
     * </p>
     */
    private int draw;
    /**
     * 查询起始位置
     */
    private int start;
    /**
     * 预读长度 = 预读页数 * 每页行数
     */
    private int length;
    /**
     * Global search
     */
    private Search search;
    private List<Column> columns;
    private List<Order> order;

    /**
     * 当前页码
     *
     * @version 2017-04-11 10:47
     */
    private int pageNo;
    /**
     * 每页显示数据条数，与 {@link #length} 相等
     *
     * @version 2017-04-11 10:47
     */
    private int pageSize;

    /**
     * 请求数据中的一个随机数：_
     */
    private long timestamp;

    // public DatatablesRequest() {
    // this.draw = 0;
    // this.start = 0;
    // this.length = 0;
    // this.search = new Search();
    // this.order = new ArrayList<>(1);
    // this.columns = new ArrayList<>(10);
    // }

    // DatatablesUtils 与 DatatablesRequest 同一个包，这个构造方法改为 private，不太好看
    public DatatablesRequest(int draw, int start, int length, Search search, List<Column> columns, List<Order> order) {
        this.draw = draw;
        this.start = start;
        this.length = length;
        // search 不能为 NULL，search.value 可以为 NULL；下同 2017-04-10 10:32:15
        this.search = search == null ? new Search() : search;
        this.order = order == null ? new ArrayList<Order>() : order;
        this.columns = columns == null ? new ArrayList<Column>() : columns;
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    public List<Order> getOrder() {
        return order;
    }

    public void setOrder(List<Order> order) {
        this.order = order;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}
